package tech.xiaozai.exam.rest;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tech.xiaozai.common.CommonPage;
import tech.xiaozai.common.restful.RestResp;
import tech.xiaozai.exam.bo.ExamQuestionBO;
import tech.xiaozai.exam.domain.ExamQuestionInfo;

import tech.xiaozai.exam.po.ExamSubject;
import tech.xiaozai.exam.service.ExamQuestionService;


import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-10-07 10:58
 */
@RestController
public class ExamQuestionController {

    @Autowired
    private ExamQuestionService examQuestionService;

//    @ApiOperation(value = "添加试题")
//    @RequestMapping(value = "/questions",method = RequestMethod.POST)
//    public RestResp addExamQuestion(ExamQuestionBO question){
//        examQuestionService.insert(question);
//        return RestResp.ok();
//    }


    @ApiOperation(value = "获取所有科目信息")
    @RequestMapping(value = "/subjects", method = RequestMethod.GET)
    public RestResp<List<ExamSubject>> getAllSubjects(){
        return RestResp.ok( examQuestionService.getSubjects() );
    }


    @ApiOperation(value = "批量添加试题")
    @RequestMapping(value = "/questions",method = RequestMethod.POST)
    public RestResp addExamQuestion(@RequestBody LinkedList<ExamQuestionBO> questions){
        System.out.println("questions="+questions);
        examQuestionService.batchInsert(questions);
        return RestResp.ok();
    }


    @ApiOperation(value = "获取一道试题")
    @RequestMapping(value = "/questions/{id}",method = RequestMethod.GET)
    public RestResp<ExamQuestionBO> getExamQuestion(Long id){
        return RestResp.ok(examQuestionService.get(id));
    }


    @ApiOperation(value = "分页获取试题")
    @RequestMapping(value = "/questions",method = RequestMethod.GET)
    public RestResp<CommonPage<ExamQuestionBO>> page(ExamQuestionInfo questionInfo){
        return RestResp.ok( examQuestionService.page(questionInfo.getPageNum(),questionInfo.getPageSize(),questionInfo));
    }

    @ApiOperation(value = "修改一道试题")
    @RequestMapping(value = "/questions",method = RequestMethod.PUT)
    public RestResp<ExamQuestionBO> update(ExamQuestionBO examQuestion){
        examQuestionService.update(examQuestion);
        return RestResp.ok(examQuestionService.get(examQuestion.getId()));
    }

    @ApiOperation(value = "删除试题")
    @RequestMapping(value = "/questions",method = RequestMethod.DELETE)
    public RestResp delete(List<Long> ids){
        examQuestionService.delete(ids);
        return RestResp.ok();
    }


}
